home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP12.ZIP / PATRON / PAGEMOUS.CPP < prev    next >
C/C++ Source or Header  |  1993-06-27  |  19KB  |  763 lines

  1. /*
  2.  * PAGEMOUS.CPP
  3.  * Modifications for Chapter 12
  4.  *
  5.  * Implementation of mouse-related member functions of CPage.
  6.  * The remainder is in PAGE.CPP.  This separate file keeps this
  7.  * grungy hit-testing/drawing code out of our way.
  8.  *
  9.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  10.  *
  11.  * Kraig Brockschmidt, Software Design Engineer
  12.  * Microsoft Systems Developer Relations
  13.  *
  14.  * Internet  :  kraigb@microsoft.com
  15.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  16.  */
  17.  
  18.  
  19. #include "patron.h"
  20.  
  21.  
  22. //Lookups into the array using g_rgHTCode[x+y*3] in PAGEMOUS.CPP
  23. #define YTOP            0
  24. #define YMID            1
  25. #define YBOT            2
  26. #define XLEFT           0
  27. #define XMID            1
  28. #define XRIGHT          2
  29.  
  30. //Values to restrict sizing in CPage::OnMouseMove
  31. #define SIZINGTOP       0x0001
  32. #define SIZINGBOTTOM    0x0002
  33. #define SIZINGLEFT      0x0004
  34. #define SIZINGRIGHT     0x0008
  35.  
  36.  
  37. //This array is for hit-testing lookups
  38. static UINT g_rgHTCode[9]={HTTOPLEFT, HTTOP, HTTOPRIGHT
  39.     , HTLEFT, HTCLIENT, HTRIGHT, HTBOTTOMLEFT, HTBOTTOM, HTBOTTOMRIGHT};
  40.  
  41.  
  42. //This is for restricting tracking based on the hit-test
  43. static UINT g_rguSizingFlags[9]={SIZINGTOP | SIZINGLEFT, SIZINGTOP
  44.     , SIZINGTOP | SIZINGRIGHT, SIZINGLEFT, 0, SIZINGRIGHT
  45.     , SIZINGBOTTOM | SIZINGLEFT, SIZINGBOTTOM, SIZINGBOTTOM | SIZINGRIGHT};
  46.  
  47.  
  48. /*
  49.  * CPage::OnRightDown
  50.  *
  51.  * Purpose:
  52.  *  Called when the user clicks with the right button on this page.
  53.  *  If there is an object here, determined by the last hit-test code,
  54.  *  the we'll make a popup-menu for it.
  55.  *
  56.  * Parameters:
  57.  *  uKeys           UINT carrying the key state.
  58.  *  x, y            UINT coordinates of the click in device units.
  59.  *
  60.  * Return Value:
  61.  *  BOOL            Indicates if the action changed the object.
  62.  */
  63.  
  64. BOOL CPage::OnRightDown(UINT uKeys, UINT x, UINT y)
  65.     {
  66.     HMENU       hMenu;
  67.     HMENU       hMenuRes;
  68.     HINSTANCE   hInst;
  69.     HWND        hWndFrame, hWndT;
  70.     POINT       pt;
  71.     UINT        i, cItems;
  72.  
  73.     //Select the tenant under the mouse, if there is one.
  74.     if (!FSelectTenantAtPoint(x, y))
  75.         return FALSE;
  76.  
  77.     /*
  78.      * Get the top-level window to which menu command will go.  This will
  79.      * be whatever parent doesn't have a parent itself...
  80.      */
  81.     hWndT=GetParent(m_hWnd);
  82.  
  83.     while (NULL!=hWndT)
  84.         {
  85.         hWndFrame=hWndT;
  86.         hWndT=GetParent(hWndT);
  87.         }
  88.  
  89.     /*
  90.      * Build a popup menu for this object with Cut, Copy, Delete, and
  91.      * object verbs.
  92.      */
  93.     hInst=GETWINDOWINSTANCE(m_hWnd);    //Macro in WIN1632.H
  94.     hMenuRes=LoadMenu(hInst, MAKEINTRESOURCE(IDR_RIGHTPOPUPMENU));
  95.  
  96.     if (NULL==hMenuRes)
  97.         return FALSE;
  98.  
  99.     //Resource-loaded menus don't work, so we'll copy the items.
  100.     hMenu=CreatePopupMenu();
  101.     cItems=GetMenuItemCount(hMenuRes);
  102.  
  103.     for (i=0; i < cItems; i++)
  104.         {
  105.         char        szTemp[80];
  106.         int         id, uFlags;
  107.  
  108.         GetMenuString(hMenuRes, i, szTemp, sizeof(szTemp), MF_BYPOSITION);
  109.         id=GetMenuItemID(hMenuRes, i);
  110.  
  111.         uFlags=(0==id) ? MF_SEPARATOR : MF_STRING | MF_ENABLED;
  112.         AppendMenu(hMenu, uFlags, id, szTemp);
  113.         }
  114.  
  115.     DestroyMenu(hMenuRes);
  116.  
  117.     //Munge the Object menu item
  118.     m_pTenantCur->AddVerbMenu(hMenu, MENUPOS_OBJECTONPOPUP);
  119.  
  120.     //CHAPTER12MOD
  121.     //Enable or disable the Links item.
  122.     i=FQueryLinksInPage() ? MF_ENABLED : MF_DISABLED | MF_GRAYED;
  123.     EnableMenuItem(hMenu, IDM_EDITLINKS, i | MF_BYCOMMAND);
  124.     //End CHAPTER12MOD
  125.  
  126.     SETPOINT(pt, x, y);
  127.     ClientToScreen(m_hWnd, &pt);
  128.  
  129.     TrackPopupMenu(hMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON
  130.         , pt.x, pt.y, 0, hWndFrame, NULL);
  131.  
  132.     DestroyMenu(hMenu);
  133.     return FALSE;
  134.     }
  135.  
  136.  
  137.  
  138.  
  139. /*
  140.  * CPage::FSelectTenantAtPoint
  141.  *
  142.  * Purpose:
  143.  *  Selects whatever tenant is at the point (x,y) if there is one,
  144.  *  deselecting the previously selected tenant.
  145.  *
  146.  * Parameters:
  147.  *  x, y            UINT coordinates of the mouse.
  148.  *
  149.  * Return Value:
  150.  *  BOOL            TRUE if there is a tenant here, FALSE otherwise.
  151.  */
  152.  
  153. BOOL CPage::FSelectTenantAtPoint(UINT x, UINT y)
  154.     {
  155.     UINT            iTenant;
  156.     LPTENANT        pTenant;
  157.  
  158.     iTenant=TenantFromPoint(x, y, &pTenant);
  159.  
  160.     if (NULL==pTenant)
  161.         return FALSE;
  162.  
  163.     //If this one is already current, we might be now sizing.
  164.     if (pTenant==m_pTenantCur)
  165.         return TRUE;
  166.  
  167.     //Deselect the current tenant
  168.     if (NULL!=m_pTenantCur)
  169.         m_pTenantCur->Select(FALSE);
  170.  
  171.     //Move this tenant to the top of the list
  172.     m_iTenantCur=0;
  173.  
  174.     SendMessage(m_hWndTenantList, LB_DELETESTRING, iTenant, 0L);
  175.     SendMessage(m_hWndTenantList, LB_INSERTSTRING, 0, (LONG)pTenant);
  176.  
  177.     //Select and repaint the new tenant to show it up front
  178.     m_pTenantCur=pTenant;
  179.  
  180.     m_pTenantCur->Repaint();
  181.     m_pTenantCur->Select(TRUE);
  182.  
  183.     return TRUE;
  184.     }
  185.  
  186.  
  187.  
  188.  
  189. /*
  190.  * CPage::OnLeftDown
  191.  *
  192.  * Purpose:
  193.  *  Called when the user clicks with the left button on this page.
  194.  *  We find the object under that position that is visibly on top
  195.  *  (always the first one under this location in the page list since
  196.  *  we paint in reverse order) and select it.
  197.  *
  198.  * Parameters:
  199.  *  uKeys           UINT carrying the key state.
  200.  *  x, y            UINT coordinates of the click in device units.
  201.  *
  202.  * Return Value:
  203.  *  BOOL            Indicates if the action changed the object.
  204.  */
  205.  
  206. BOOL CPage::OnLeftDown(UINT uKeys, UINT x, UINT y)
  207.     {
  208.     RECT        rc;
  209.  
  210.     if (HTCAPTION==m_uHTCode)
  211.         return DragDrop(uKeys, x, y);
  212.  
  213.     /*
  214.      * See if we have to start sizing (which always happens on current
  215.      * tenant).  m_uHTCode is set in OnNCHitTest below.
  216.      */
  217.     if (HTNOWHERE!=m_uHTCode && HTCLIENT!=m_uHTCode)
  218.         {
  219.         //We are sizing, so start tracking
  220.         m_pTenantCur->RectGet(&m_rcl, TRUE);
  221.         SetCapture(m_hWnd);
  222.         m_fTracking=TRUE;
  223.  
  224.         m_hDC=GetDC(m_hWnd);
  225.  
  226.         //Place the rectangle exactly where it is on the screen.
  227.         RECTFROMRECTL(rc, m_rcl)
  228.         OffsetRect(&rc, -(int)m_pPG->m_xPos, -(int)m_pPG->m_yPos);
  229.         RECTLFROMRECT(m_rcl, rc);
  230.         m_rclOrg=m_rcl;
  231.  
  232.         DrawFocusRect(m_hDC, &rc);
  233.  
  234.         m_pPG->CalcBoundingRect(&rc, TRUE);
  235.         RECTLFROMRECT(m_rclBounds, rc);
  236.         return FALSE;
  237.         }
  238.  
  239.     //Selection logic moved to a function so OnRightDown can use it.
  240.     FSelectTenantAtPoint(x, y);
  241.  
  242.     return FALSE;
  243.     }
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251. /*
  252.  * CPage::OnLeftUp
  253.  *
  254.  * Purpose:
  255.  *  Called when the user clicks up with the left button on this page.
  256.  *  We stop tracking on this message, if necessary, and resize the object.
  257.  *
  258.  * Parameters:
  259.  *  uKeys           UINT carrying the key state.
  260.  *  x, y            UINT coordinates of the click in device units.
  261.  *
  262.  * Return Value:
  263.  *  BOOL            Indicates if this action changed the object.
  264.  */
  265.  
  266. BOOL CPage::OnLeftUp(UINT uKeys, UINT x, UINT y)
  267.     {
  268.     RECT    rc, rcT;
  269.  
  270.     if (!m_fTracking)
  271.         return FALSE;
  272.  
  273.     //Remove the dotted rectangle.
  274.     RECTFROMRECTL(rc, m_rcl)
  275.     DrawFocusRect(m_hDC, &rc);
  276.     ReleaseDC(m_hWnd, m_hDC);
  277.  
  278.     ReleaseCapture();
  279.     m_fTracking=FALSE;
  280.  
  281.     //If the original and new rects are the same, nothing happened.
  282.     RECTFROMRECTL(rcT, m_rclOrg);
  283.  
  284.     if (EqualRect(&rc, &rcT))
  285.         return FALSE;
  286.  
  287.     RECTFROMRECTL(rcT, m_rclOrg);
  288.     InvalidateRect(m_hWnd, &rcT, TRUE);
  289.  
  290.     //Invalidate on the screen before accounting for scrolling
  291.     InvalidateRect(m_hWnd, &rc, TRUE);
  292.  
  293.     //Factor in the scrolling and tell the tenant where it now stands.
  294.     OffsetRect(&rc, (int)m_pPG->m_xPos, (int)m_pPG->m_yPos);
  295.     RECTLFROMRECT(m_rcl, rc);
  296.     m_pTenantCur->RectSet(&m_rcl, TRUE);
  297.  
  298.     UpdateWindow(m_hWnd);
  299.     return TRUE;
  300.     }
  301.  
  302.  
  303.  
  304.  
  305.  
  306. /*
  307.  * CPage::OnLeftDoubleClick
  308.  *
  309.  * Purpose:
  310.  *  Called when the user double-clicks with the left button on this page.
  311.  *  We find the object under that position that is visibly on top
  312.  *  (always the first one under this locatio